package com.tedu.goods.service.impl;

import com.tedu.common.response.JsonResult;
import com.tedu.goods.mapper.GoodsMapper;
import com.tedu.goods.pojo.dto.GoodsParam;
import com.tedu.goods.pojo.dto.GoodsQuery;
import com.tedu.goods.pojo.entity.Goods;
import com.tedu.goods.pojo.vo.GoodsVO;
import com.tedu.goods.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {
    @Autowired  GoodsMapper goodsMapper;

    @Override
    public List<GoodsVO> selectGoods(GoodsQuery goodsQuery) {
        log.debug("查询商品列表业务,参数:{}",goodsQuery);
        List<GoodsVO> list = goodsMapper.selectGoods(goodsQuery);
        log.debug("查询商品列表业务,结果:{}",list);
        return list;
    }

    @Override
    public void saveGoods(GoodsParam goodsParam) {
        log.debug("保存商品信息,参数:{}",goodsParam);
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsParam,goods);
        if(goodsParam.getId() == null){//新增
            goods.setCreateTime(new Date());
            goodsMapper.insert(goods);
        }else{//编辑
            log.debug("编辑商品信息,参数:{}",goods);
           goods.setUpdateTime(new Date());
            goodsMapper.update(goods);
        }
    }

    @Override
    public void deleteGoods(Long id) {
        log.debug("删除商品信息,参数:={}",id);
        goodsMapper.deleteGoods(id);
    }

    @Override
    public void updateStatus(Integer goodsId, String status) {
        log.debug("修改商品状态业务,参数:{},{}",goodsId,status);
        Goods goods = new Goods();
        goods.setId(goodsId);
        goods.setStatus(status);
        goods.setUpdateTime(new Date());
        goodsMapper.update(goods);
    }
}
